﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------

using System.ComponentModel;
using System.ServiceModel;
using System.Windows.Controls;
using System.Windows.Navigation;
using ControlTestApplications.Silverlight.Dashboard.GatewayService;

namespace ControlTestApplications.Silverlight.Dashboard
{
    public partial class AdvertisingSample : Page
    {

        #region Private Fields

        string errorMessage;

        #endregion

        #region Ctor

        public AdvertisingSample()
        {
            InitializeComponent();
            Loaded += (s, e) =>
            {
                LoadAdvertisements();
            };
        }

        #endregion

        private void LoadAdvertisements()
        {

            GatewayServiceClient client = new GatewayServiceClient();
            client.GetAdvertisementCompleted += (s, e) =>
            {
                if (!HasError(e))
                {
                    var advert = e.Result;
                    if (advert != null)
                    {

                        if (advert.AdType == AdType.Image) // first control
                        {
                            advertControl1.AdvertisementImageSrc = advert.ImageUrl;
                            advertControl1.AdvertisementMode = Microsoft.Telco.Controls.AdvertisementMode.Image;
                            advertControl1.NavigateUrl = advert.NavigateUrl;
                            advertControl1.AdvertisementImageAtlText = string.Empty;
                            advertControl1.AdvertisementText = string.Empty;
                            advertControl1.AdvMessageTextblock.Text = string.Empty;
                            advertControl1.OnApplyTemplate();
                        }
                        else
                        {
                            advertControl2.AdvertisementImageSrc = advert.Text;
                            advertControl2.AdvertisementMode = Microsoft.Telco.Controls.AdvertisementMode.Text;
                            advertControl2.NavigateUrl = advert.NavigateUrl;
                            advertControl2.AdvertisementImageAtlText = string.Empty;
                            advertControl2.AdvertisementText = advert.Text;
                            advertControl2.OnApplyTemplate();
                        }
                    
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(errorMessage))
                    {
                        // Ignore for now since we're not sure which control to update
                        tbException.Text = errorMessage;
                    }
                }
            };

            // Request two advertisements, one text and one image
            client.GetAdvertisementAsync(new AdvertisementRequest() { AdProtectionPolicy = AdProtectionPolicy.Low, AdType = AdType.Image });
            client.GetAdvertisementAsync(new AdvertisementRequest() { AdProtectionPolicy = AdProtectionPolicy.Low, AdType = AdType.Text });

        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private bool HasError(AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                if (e.Error is FaultException<AuthenticationFault>)
                {
                    //Authentication required
                    var fault = e.Error as FaultException<AuthenticationFault>;
                    ChildWindow authWin = new AuthPrompt(fault.Detail.ConsentUrl);
                    authWin.Show();

                    errorMessage = null;
                    return true;
                }
                else if (e.Error is FaultException<TelcoFault>)
                {
                    var fault = e.Error as FaultException<TelcoFault>;
                    errorMessage = fault.Detail.Message;
                }
                else
                {
                    errorMessage = e.Error.Message;
                }
                return true;
            }
            return false;
        }
 

    }
}